package com.xxh.springclouddemo.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * @Author xxh
 * @Date 2024/4/20 19:42
 */
@Component
public class GateWayFilter implements GlobalFilter, Ordered {

    /**
     * 过滤器逻辑处理
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入过滤器！！！！");
        //获取请求对象
        ServerHttpRequest request = exchange.getRequest();

        //获取返回对象
        ServerHttpResponse response = exchange.getResponse();

        //获取请求路径
        URI uri = request.getURI();
        RequestPath path = request.getPath();

        System.err.println("url="+uri+"~~~~~~~~~path="+path);

        //放行
        return chain.filter(exchange);
    }


    /**
     * 设置路由执行顺序，数字越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
